package util;

import javax.swing.JTextArea;

public class IOUtil {
	
	final public static int DEFAULT_READ_BUFFER_SIZE = 8192;
	final public static String DEFAULT_DELIMITER = "$";
	final public static String DEFAULT_NEWLINE = "\n";
	
	final public static String CMD_RESUME = "CMD_RESUME";
	final public static String CMD_OVERWRITE = "CMD_OVERWRITE";
	final public static String CMD_CANCEL = "CMD_CANCEL";
	
	final public static String CMD_SERVER_START = "CMD_SERVER_START";
	final public static String CMD_SERVER_STOP = "CMD_SERVER_STOP";
	final public static String CMD_SERVER_CHOOSE_DIR = "CMD_SERVER_CHOOSE_DIR";
	
	final public static String CMD_CLIENT_SEND_FILE = "CMD_CLIENT_SEND_FILE";
	final public static String CMD_CLIENT_CHOOSE_FILE = "CMD_CLIENT_CHOOSE_FILE";
	final public static String CMD_CLIENT_PAUSE = "CMD_CLIENT_PAUSE";
	final public static String CMD_CLIENT_RESUME = "CMD_CLIENT_RESUME";
	final public static String CMD_CLIENT_STOP = "CMD_CLIENT_STOP";
	
	final public static int MSG_TYPE_APP = 0x00;
	final public static int MSG_TYPE_CLIENT = 0x01;
	final public static int MSG_TYPE_SERVER = 0x02;
	
	final public static String MSG_INFO = "[INFO]:";
	final public static String MSG_CLIENT = "[CLIENT]:";
	final public static String MSG_SERVER = "[SERVER]:";
	
	final private JTextArea taLog;
	public IOUtil(JTextArea ta) {
		this.taLog = ta;
	}

	public static boolean isNullString(String str)
	{
		return (str == null || "".equalsIgnoreCase(str));
	}
	
	public static int getProgress(long currentSize, long totalSize)
	{
		return  (int)(currentSize*100/totalSize);
	}
	
    public void dumpClientMsg(String msg) {
    	dumpDefineMsg(MSG_CLIENT , msg);
    }
    
    public void dumpServerMsg(String msg) {
    	dumpDefineMsg(MSG_SERVER , msg);
    }
    
    public void dumpInfoMsg(String msg) {
    	dumpDefineMsg(MSG_INFO , msg);
    }
    protected void dumpDefineMsg(String header, String msg) {
    	msg = header +  msg;
    	dumpMsg(msg);
    }
    
    private void dumpMsg(String msg) {
    	System.out.println(msg);
    	taLog.append(msg + DEFAULT_NEWLINE);
    	taLog.setCaretPosition(taLog.getDocument().getLength());
    }
}
